<template>
  <div>
    <el-dialog :title="title" :visible="visible" @close="handleClose" width="600px">
      <el-form :model="frozen" label-position="right" ref="frozenRef" :rules="rules">
        <el-form-item label="是否冻结" prop="freezeStatus" :label-width="formLabelWidth">
          <el-select clearable v-model="frozen.freezeStatus" style="width: 100%;" @change="handleTypeChangeCode">
            <el-option v-for="(item, index) in freezeStatusList" :key="index" :value="item.id" :label="item.name">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item v-show="frozen.freezeStatus == 1" label="类型" prop="typeRestriction" :label-width="formLabelWidth">
          <el-select clearable v-model="frozen.typeRestriction" style="width: 100%;" @change="handleTypeChange">
            <el-option v-for="(item, index) in frozenType" :key="index" :value="item.id" :label="item.typeName">
            </el-option>
          </el-select>
        </el-form-item>

        <!-- frozen.typeRestriction == 2 显示单据类型 -->
        <el-form-item v-show="frozen.typeRestriction == 2&&frozen.freezeStatus == 1" label="单据类型" prop="typeMapId"
          :label-width="formLabelWidth">
          <el-select filterable multiple clearable v-model="frozen.typeMapId" style="width: 100%;">
            <el-option v-for="(item, index) in billList" :key="index" :value="item.tid"
              :label="`${item.msfxwarehousename}(${item.remark})`">
            </el-option>
          </el-select>
        </el-form-item>

        <!-- frozen.typeRestriction == 3 显示追溯码 -->
       <!-- <el-form-item v-show="frozen.typeRestriction == 3&&frozen.freezeStatus == 1" label="追溯码" prop="skuBarId"
          :label-width="formLabelWidth">
          <el-tree ref="treeRef" :data="pkbathcodeList" show-checkbox node-key="skuBarId" :props="defaultProps"
            @check-change="handleCheckChange"></el-tree>
        </el-form-item> -->
        <!-- frozen.typeRestriction == 4 显示库存状态 -->
        <el-form-item v-if="(frozen.typeRestriction == 1||frozen.typeRestriction == 2)&&frozen.freezeStatus" label="状态"
          prop="stateCode" :label-width="formLabelWidth">
          <el-select clearable style="width: 100%;" v-model="frozen.stateCode">
            <el-option v-for="(item, index) in statusList" :key="index" :value="item.PK_STORESTATE" :label="item.name">
            </el-option>
          </el-select>
        </el-form-item>
        <!-- frozen.typeRestriction == 1 显示限制数量 -->
        <el-form-item v-if="frozen.typeRestriction == 1&&frozen.freezeStatus == 1"
          label="数量" prop="limitQuantity" :label-width="formLabelWidth">
          <el-input v-model="frozen.limitQuantity" placeholder="请输入限制数量"></el-input>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="handleClose">取消</el-button>
        <el-button type="primary" @click="submit">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    findTypeList, //获取类型
    findTableXXXSkuList, //批号限制
    findTypeAllByName, //单据类型
    freeze //冻结提交
  } from '@/api/storeM/index.js'

  export default {
    data() {
      return {
        frozen: {
          typeRestriction: '', // typeRestriction 为空时，表单项也会被清空
          limitQuantity: '', // 用于限制数量
          skuBarId: [], // 追溯码的SKU
          typeMapId: [], // 单据类型
          freezeStatus: 0,
          stateCode: '', //状态
        },
        statusList: [], //状态
        frozenType: [], //类型
        pkbathcodeList: [], //批号
        billList: [], //单据类型
        formLabelWidth: '68px',
        title: '冻结',
        freezeStatusList: [{
            id: 1,
            name: '是'
          },
          {
            id: 0,
            name: '否'
          },
        ], //是否冻结
        defaultProps: {
          children: 'children',
          label: 'skuBarId',
          id: 'skuBarId', // 使用 skuBarId 作为唯一标识
        },
        rules: {
          limitQuantity: [{
            required: true,
            message: '请输入数量',
            trigger: 'blur'
          }],
        },
      };
    },
    created() {
      this.getTypeList(); // 获取类型
      this.getfindTypeAllByName(); // 获取单据类型
    },
    props: {
      visible: {
        type: Boolean,
        default: false,
      },
    },
    methods: {
      //获取单据类型
      getfindTypeAllByName() {
        findTypeAllByName().then(res => {
          this.billList = res.data;
        })
      },
      //获取追溯码
      getBathCode() {
        let param = {
          pkMarbasclass: this.frozen.pkMaterial,
          pkBatchcode: this.frozen.pkBatchcode,
          cwarehouseId:this.frozen.cwarehouseId,
          cproductorId:this.frozen.cproductorid
        }
        findTableXXXSkuList(param).then(res => {
          if (res.msg == '该表不存在') {
            this.frozenType = this.frozenType.filter(item => item.id !== 3);
          } else {
            this.pkbathcodeList = res.data.vos;
            this.statusList = res.data.status;
          }
        })
      },
      //获取类型信息
      getTypeList() {
        findTypeList().then(res => {
          this.frozenType = res.data;
        })
      },
      //选择树形checkbox
      handleCheckChange(checkedKeys) {
        const checkedNodes = this.$refs.treeRef.getCheckedNodes();
        this.frozen.skuBarId = checkedNodes.map(item => item.skuBarId);
      },
      openDiolog(row) {
        if (row) {
          this.frozen.vbatchcode = row.vbatchcode;//批次号
          this.frozen.name = row.name;//商品名称
          this.frozen.pkMaterial = row.pk_material;
          this.frozen.pkBatchcode = row.pk_batchcode;
          this.frozen.cwarehouseId = row.cwarehouse_id;//库房
          this.frozen.cproductorid = row.pk_product?row.pk_product:'';//生产厂商
          this.frozen.typeRestriction = ''; // 状态不是特定的时，清空类型
          this.frozen.limitQuantity = ''; // 清空限制数量
          this.frozen.typeMapId = []; // 清空单据类型
          this.frozen.skuBarId = []; // 清空追溯码
          this.frozen.stateCode = ''; // 状态
          this.statusList = []; // 清空旧数据
        }
        this.getBathCode(); //点击冻结后调用追溯码list获取信息
      },
      handleClose() {
        this.$emit('update:visible', false);
        this.$nextTick(() => {
          this.$refs.frozenRef.resetFields();
        })
      },
      submit() {
        this.$refs.frozenRef.validate((valid) => {
          if (valid) {
            const apiMethod = freeze;
            apiMethod(this.frozen).then(res => {
              this.$message({
                message: res.msg,
                type: 'success',
              });
              this.$emit('successfun');
              this.handleClose();
            }).catch(err => {
              this.$message({
                message: err?.msg || '操作失败',
                type: 'error',
              });
            });
          } else {
            return false;
          }
        });
      },

      //当冻结变化时候
      handleTypeChangeCode(data) {
        if (data == '0') {
          this.frozen.typeMapId = [];
          this.frozen.skuBarId = [];
          this.frozen.stateCode = '';
          this.frozen.limitQuantity = '';
        }
      },
      // 当类型改变时，根据条件清空对应字段
      handleTypeChange(newType) {
        // 清空其他字段
        if (newType === 1) {
          this.frozen.typeMapId = [];
          this.frozen.skuBarId = [];
          this.frozen.stateCode = '';
          this.frozen.limitQuantity = '';
        } else if (newType === 2) {
          this.frozen.limitQuantity = '';
          this.frozen.stateCode = '';
          this.frozen.skuBarId = [];
        } else if (newType === 3) {
          this.frozen.stateCode = '';
          this.frozen.limitQuantity = '';
          this.frozen.typeMapId = [];
        }
        this.$nextTick(() => {
          this.$refs.frozenRef.clearValidate();
        });
      }
    },
  };
</script>

<style scoped>
  /deep/.el-tree {
    height: 300px;
    overflow: auto;
  }
</style>
